Latest update: July 2013
In this tutorial, we'll show you how to fetch a list of files from your FlashAir with an iOS App. We'll use command.cgi to do this.
We will create an application that retrieves information about files in a specified directory of the FlashAir and displays the results on the screen.
We will use
StoryBoards in this tutorial, but here is not explained how to use
StoryBoards. If you will use it for the first time, try to develop using
examples from
the
apple developer website
and the actual Sample Code.
Here is the screen layout of the app we want to create:
We will set the app so that if the "Get List" button is tapped, the number of files in the directory will appear in the "labelCount" box and the file list will display in the "textViewList" box.
To do this, we will add the following as class
property objects:
UIButton)
UILabel)
UITextView)
We will also declare a function in the
FSViewController class that will do some action when it detects the
UIButton has been pressed. (We will write the implementation for this function
later in
this tutorial.)
The layout elements will be declared in FSViewController.h. The modified file will look like this:
#import <UIKit/UIKit.h>
@interface FSViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *labelCount;
@property (strong, nonatomic) IBOutlet UITextView *textViewList;
- (IBAction)buttonPush:(id)sender;
@end
To get the file list, we will use
command.cgi with
op=100. Since we need to specify the folder from which we wish to fetch data,
we will
supply the
DCIM folder.
In order to execute this cgi command, we will use
NSString stringWithContentsOfURL
. This
NSString function will allow us to get the information at the specified URL in
UTF-8 encoding
and will return any error data in an
NSError object.
#import "FSViewController.h"
@interface FSViewController ()
@end
@implementation FSViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.labelCount.text = @"";
self.textViewList.text = @"";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)buttonPush:(id)sender {
NSError *error = nil;
self.labelCount.text = @"";
self.textViewList.text = @"";
// Get file list
// Make url
NSURL *url100 =
[NSURL URLWithString:@"http://flashair/command.cgi?op=100&DIR=/DCIM"];
// Run cgi
NSString *dirStr =
[NSString stringWithContentsOfURL:url100 encoding:NSUTF8StringEncoding error:&error];
if ([error.domain isEqualToString:NSCocoaErrorDomain]){
NSLog(@"error100 %@\n",error);
return;
}
// Display results
self.textViewList.text = dirStr;
op=100&DIR=/DCIM. encoding:NSUTF8StringEncoding
textViewList. To get the number of files, we use
command.cgi with
op=101. Just as when we retrieved the file list, when getting the number of
files, we
will need to specify a directory. We will specify
DCIM as the target directory again.
// Make url
NSURL *url101 =
[NSURL URLWithString:@"http://flashair/command.cgi?op=101&DIR=/DCIM"];
// Run cgi
NSString *cntStr =
[NSString stringWithContentsOfURL:url101 encoding:NSUTF8StringEncoding error:&error];
if ([error.domain isEqualToString:NSCocoaErrorDomain]) {
NSLog(@"error101 %@\n",error);
return;
}
// Display results
self.labelCount.text =[@"Count=" stringByAppendingString:cntStr];
}
op=101 and
DIR=/DCIM as parameters in our URL. LabelCount. Once the program is complete, we will check to see if it works. When you tap the "Get List" button, the number of files and list of the directory contents should be displayed.
You have now completed the tutorial on getting a content list.
ios_tutorial_02.zip (21KB)
All sample code on this page is licensed under BSD 2-Clause License